#include "movieListRes.h"
#include "jsonUtils.h"

MovieListRes::MovieListRes() : OperatorResponse(true),total(0),size(0),current(0),pages(0)
{

}

MovieListRes::MovieListRes(int total, int size, int current, int pages, const std::list<Movie*>& list)
	:OperatorResponse(true),total(total),size(size),current(current),pages(pages),records(list)
{

}

JsonDoc& MovieListRes::jsonSerialize()
{
	JsonDoc& doc = OperatorResponse::jsonSerialize();
	JsonValue v(rapidjson::kObjectType);
	JsonDoc::AllocatorType& allocator = doc.GetAllocator();
	v.AddMember(L"total", total, allocator);
	v.AddMember(L"size", size, allocator);
	v.AddMember(L"current", current, allocator);
	v.AddMember(L"pages", pages, allocator);

	JsonValue arr(rapidjson::kArrayType);

	if (!records.empty()) {
		for (std::list<Movie*>::const_iterator it = records.begin();it != records.end();++it) {
			JsonValue item(rapidjson::kObjectType);
			(*it)->toJson(item, allocator);
			arr.PushBack(item,allocator);
		}
	}
	v.AddMember(L"records", arr, allocator);
	doc.AddMember(L"data", v, allocator);
	return doc;
}
